#include "Header.h"

#define USART_RX_LEN (uint32_t)7

uint8_t state;
uint8_t maxstate;
uint8_t ExamByte;
uint8_t num;

uint8_t DataArray_R[50];

uint8_t flag;

/*HC05初始化*/
void HC05_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

    GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = TX;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = RX;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_Init(USART1, &USART_InitStructure);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
    USART_ClearFlag(USART1,USART_FLAG_TC);

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(USART1, ENABLE);
}

/*发送一个字节的数据*/
void HC05_SendByte(uint8_t data)
{
    USART_SendData(USART1, data);
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

/*发送一个字符*/
void HC05_SendChar(char data)
{
    USART_SendData(USART1, (uint8_t)data);
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

/*发送8位整型*/
uint8_t HC05_SendData8(int8_t data)
{
    HC05_SendByte(*(uint8_t*)&data);
    return *(uint8_t*)&data;
}

/*发送16位整型*/
uint8_t HC05_SendData16(int16_t data)
{
    HC05_SendByte(*(uint8_t*)&data);
    HC05_SendByte(*((uint8_t*)&data + 1));
    return *(uint8_t*)&data + *((uint8_t*)&data + 1);
}

/*发送32位整型*/
uint8_t HC05_SendData32(int32_t data)
{
    HC05_SendByte(*(uint8_t*)&data);
    HC05_SendByte(*((uint8_t*)&data + 1));
    HC05_SendByte(*((uint8_t*)&data + 2));
    HC05_SendByte(*((uint8_t*)&data + 3));
    return *(uint8_t*)&data + *((uint8_t*)&data + 1) + *((uint8_t*)&data + 2) + *((uint8_t*)&data + 3);
}

/*发送浮点数*/
uint8_t HC05_SendDataFloat(float data)
{
    int32_t data_u32 = *(int32_t*)&data;
    return HC05_SendData32(data_u32);
}

/*发送字符串（配合sprintf使用）*/
void HC05_SendString(char *String)
{
    uint8_t i = 0;
    while(String[i] != '\0')
    {
        HC05_SendChar(String[i]);
        i++;
    }
}

/*发送数据包，数据包结构为0xA5(包头),原数据,校验字节,0x5A(包尾)*/
void HC05_SendDataArray(void)
{
    HC05_SendData8(0xA5);
    ExamByte += HC05_SendData16(Encoder_Speed1);
    ExamByte += HC05_SendData16(target);
    HC05_SendByte(ExamByte);
    HC05_SendData8(0x5A); 
    ExamByte = 0;
}

/*处理接收到的数据包，数据包结构为0xA5(包头),原数据,校验字节,0x5A(包尾)*/
void HC05_RecieveDataArray(void)
{
    uint8_t sum = 0;
    for(int i=0;i<num;i++)
    {
        uint8_t data = DataArray_R[i];
        HC05_SendByte(data);
        if(state == 0 && data == 0xA5)
        {
            state ++;
        }
        else if(state >= 1 && state <= 14)
        {
            sum += data;
            state ++;
        }
        else if(state == 15)
        {
            state ++;
        }
        else if(state == 16 && data == 0x5A)
        {
            state = 0;
            target = *(int16_t*)&DataArray_R[i-15];
            kp = *(float*)&DataArray_R[i-13];
            ki = *(float*)&DataArray_R[i-9];
            kd = *(float*)&DataArray_R[i-5];
            flag = 1;
        }
        else state = 0;
        if(state > maxstate) maxstate = state;
    }
    num = 0;
}

void USART1_IRQHandler(void)
{
    if(USART_GetITStatus(USART1, USART_IT_IDLE) == SET)
    {
        USART_ReceiveData(USART1); 
        USART_ClearFlag(USART1, USART_FLAG_TC);
        HC05_RecieveDataArray();
    }

    else if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
    {
        DataArray_R[num++] = USART_ReceiveData(USART1);
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}
